# coding=utf-8


from Config import config
import Replacer.GPIO as GPIO


def init():
    # 初始化要使用的端口
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup([config.switch_0, config.switch_1, config.switch_2, config.switch_3], GPIO.OUT)
    GPIO.output([config.switch_0, config.switch_1, config.switch_2, config.switch_3], GPIO.HIGH)


def uninit():
    GPIO.cleanup()


def control(switch_id, result):
    print(switch_id, result)
    # 进行对口映射
    if switch_id == 0:
        gpio_id = config.switch_0
    elif switch_id == 1:
        gpio_id = config.switch_1
    elif switch_id == 2:
        gpio_id = config.switch_2
    elif switch_id == 3:
        gpio_id = config.switch_3
    else:
        raise ValueError("未识别的 switch_id")

    print(__name__ + "  gpio_id:" + str(gpio_id) + "  result:" + str(result))

    if result:
        GPIO.output(gpio_id, GPIO.LOW)
    else:
        GPIO.output(gpio_id, GPIO.HIGH)


if __name__ == '__main__':
    try:
        init()
    except KeyboardInterrupt as e:
        uninit()

